package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.UserMapper;
import com.itheima.health.pojo.*;
import com.itheima.health.service.RoleService;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.SetmealVO;
import com.itheima.health.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RoleService roleService;

    @Override
    public UserVO findUserInfoByUsername(String username) {
        //     根据账号查询 用户对应角色信息
          UserVO  userVO= baseMapper.findUserByUsername(username);
          //  根据用户id  查询对应得角色信息
        List<RoleVO> roleVOList =  baseMapper.findRoleInfoByUserId(userVO.getId());
        //  权限信息
        for (RoleVO roleVO : roleVOList) {
            List<Permission> permissionList = baseMapper.findPermissionInfoByRoleId(roleVO.getId());
            roleVO.setPermissionsList(permissionList);
        }
          userVO.setRoleVOList(roleVOList);
        return userVO;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageData =null;
        //  条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("is_delete",0);
        //  1  mp  分页查询   完成分页查询   select  *  from   xxx  limit     count(1)
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            //  无条件分页
            //  page  结合 分页插件
            pageData   = page(new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else{
           wrapper.eq("username",queryPageBean.getQueryString());
            pageData   = page(new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }
        // 2  结果集封装 PageResult返回
        PageResult pageResult = new PageResult(pageData.getTotal(),pageData.getRecords());
        return pageResult;
    }

    @Override
    public Boolean addAndUpdate(UserVO userVO) {
        //增加或者修改
            Integer id = userVO.getId();
        boolean flag=true;
            if(id==null){
                flag = save(userVO);
            }else {
                //更新操作，先把之前的角色关系清楚
                flag = saveOrUpdate(userVO);
                baseMapper.deleteRole(id);
            }

            Integer[] roleIds = userVO.getRoleIdList();
            Integer uid = userVO.getId();
            if(roleIds.length!=0){
                for (Integer rid : roleIds) {
                    //挨个插入用户对应的角色
                    baseMapper.add(uid,rid);
                }
            }
            return flag;
        }

    @Override
    public UserVO findroleandgroleids(Integer id) {
        UserVO userVO  = baseMapper.findUserVO(id);
        List<Integer> fnndallrolelist = baseMapper.fnndallrolelist(id);
        userVO.setFnndallgrouplist(fnndallrolelist);
        List<Role> list = roleService.list();
        userVO.setRoleList(list);
        return userVO;
    }

    //根据用户名查询菜单信息
    @Override
    public List<Menu> findMenuInfoByUsername(String username) {
        //根据账号查询 用户对应角色信息
        UserVO userVO = baseMapper.findUserByUsername(username);
        //根据用户id  查询对应得角色信息
        List<RoleVO> roleVOList = baseMapper.findRoleInfoByUserId(userVO.getId());
        if (roleVOList == null) {
            return null;
        }
        List<Menu> menuInfo = null;
        for (RoleVO roleVO : roleVOList) {
            //根据角色id查询对应的菜单信息
            menuInfo = baseMapper.findMenuInfoByRoleId(roleVO.getId());
        }
        return menuInfo;
    }

}
